<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片Base64转换工具</title>
    <link href="../css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3a0ca3;
            --gray-color: #6c757d;
            --light-gray: #f8f9fa;
            --white: #ffffff;
            --border-radius: 8px;
            --box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
            background-color: #fff;
            color: #333;
        }

        .container {
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        .card {
            background-color: var(--white);
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            padding: 20px;
        }

        .controls {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }

        .file-input-label {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 10px 20px;
            background-color: var(--primary-color);
            color: var(--white);
            border: none;
            border-radius: var(--border-radius);
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .file-input-label:hover {
            background-color: var(--secondary-color);
            transform: translateY(-2px);
        }

        button {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 10px 20px;
            border: none;
            border-radius: var(--border-radius);
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
            color: var(--white);
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
        }

        #fileInput {
            display: none;
        }

        .preview-area {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }

        @media (max-width: 768px) {
            .preview-area {
                grid-template-columns: 1fr;
            }
        }

        .image-preview {
            display: flex;
            flex-direction: column;
            min-height: 300px;
        }

        .preview-placeholder {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: var(--light-gray);
            border: 2px dashed #ccc;
            border-radius: var(--border-radius);
            color: var(--gray-color);
            font-size: 16px;
        }

        .preview-image {
            max-width: 100%;
            max-height: 300px;
            object-fit: contain;
            border-radius: var(--border-radius);
            display: none;
        }

        .output-area {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        textarea {
            width: 95%;
            min-height: 150px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: var(--border-radius);
            font-family: 'Consolas', 'Monaco', monospace;
            resize: vertical;
            background-color: var(--light-gray);
        }

        h3 {
            margin-top: 0;
            margin-bottom: 15px;
            color: var(--secondary-color);
        }

        #copyBtn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        #copyHtmlBtn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        #clearBtn {
            background: var(--gray-color);
        }

        .info {
            background-color: var(--light-gray);
            padding: 20px;
            border-radius: var(--border-radius);
            border-left: 4px solid var(--primary-color);
        }

        .info h3 {
            margin-top: 0;
        }

        .info ol {
            padding-left: 20px;
        }

        .info li {
            margin-bottom: 8px;
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        .modal-content {
            background-color: var(--white);
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 30%;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            text-align: center;
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <div class="controls">
                <label for="fileInput" class="file-input-label">
                    <i class="fa-solid fa-image" style="margin-right: 8px;"></i>
                    选择图片
                </label>
                <input type="file" id="fileInput" accept="image/*">
                <button id="copyBtn">
                    <i class="fa-solid fa-copy" style="margin-right: 8px;"></i>
                    复制Base64
                </button>
                <button id="copyHtmlBtn">
                    <i class="fa-solid fa-copy" style="margin-right: 8px;"></i>
                    复制HTML
                </button>
                <button id="clearBtn">
                    <i class="fa-solid fa-trash" style="margin-right: 8px;"></i>
                    清除
                </button>
            </div>
        </div>

        <div class="card preview-area">
            <div class="image-preview">
                <h3>图片预览</h3>
                <div class="preview-placeholder" id="previewPlaceholder">
                    请选择图片文件进行预览
                </div>
                <img id="previewImage" class="preview-image" src="" alt="图片预览">
            </div>

            <div class="output-area">
                <div>
                    <h3>Base64编码</h3>
                    <textarea id="base64Output" placeholder="Base64编码将显示在这里..." readonly></textarea>
                </div>
                <div>
                    <h3>HTML图片标签</h3>
                    <textarea id="htmlCodeOutput" placeholder="HTML代码将显示在这里..." readonly></textarea>
                </div>
            </div>
        </div>
    </div>

    <!-- 模态框 -->
    <div id="myModal" class="modal">
        <div class="modal-content">
            <span class="close">&times;</span>
            <p id="modalMessage"></p>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const fileInput = document.getElementById('fileInput');
            const previewPlaceholder = document.getElementById('previewPlaceholder');
            const previewImage = document.getElementById('previewImage');
            const base64Output = document.getElementById('base64Output');
            const htmlCodeOutput = document.getElementById('htmlCodeOutput');
            const copyBtn = document.getElementById('copyBtn');
            const copyHtmlBtn = document.getElementById('copyHtmlBtn');
            const clearBtn = document.getElementById('clearBtn');
            const modal = document.getElementById('myModal');
            const modalMessage = document.getElementById('modalMessage');
            const closeBtn = document.querySelector('.close');

            // 处理文件选择
            fileInput.addEventListener('change', function (e) {
                const file = e.target.files[0];
                if (!file) return;

                if (!file.type.match('image.*')) {
                    showModal('请选择有效的图片文件！');
                    return;
                }

                const reader = new FileReader();

                reader.onload = function (event) {
                    // 显示图片预览
                    const base64String = event.target.result;
                    previewPlaceholder.style.display = 'none';
                    previewImage.src = base64String;
                    previewImage.style.display = 'block';

                    // 显示Base64编码
                    base64Output.value = base64String;

                    // 生成并显示HTML代码
                    const htmlCode = `<img src="${base64String}" alt="Base64图片">`;
                    htmlCodeOutput.value = htmlCode;
                };

                reader.onerror = function () {
                    showModal('读取文件时发生错误，请重试！');
                };

                reader.readAsDataURL(file);
            });

            // 复制Base64编码
            copyBtn.addEventListener('click', function () {
                if (!base64Output.value) {
                    showModal('没有可复制的内容！');
                    return;
                }

                base64Output.select();
                document.execCommand('copy');

                // 显示复制成功提示
                const originalText = copyBtn.innerHTML;
                copyBtn.innerHTML = `
                    <i class="fa-solid fa-check" style="margin-right: 8px;"></i>
                    已复制！
                `;
                setTimeout(() => {
                    copyBtn.innerHTML = originalText;
                }, 2000);
            });

            // 复制HTML代码
            copyHtmlBtn.addEventListener('click', function () {
                if (!htmlCodeOutput.value) {
                    showModal('没有可复制的内容！');
                    return;
                }

                htmlCodeOutput.select();
                document.execCommand('copy');

                // 显示复制成功提示
                const originalText = copyHtmlBtn.innerHTML;
                copyHtmlBtn.innerHTML = `
                    <i class="fa-solid fa-check" style="margin-right: 8px;"></i>
                    已复制！
                `;
                setTimeout(() => {
                    copyHtmlBtn.innerHTML = originalText;
                }, 2000);
            });

            // 清除所有内容
            clearBtn.addEventListener('click', function () {
                fileInput.value = '';
                previewPlaceholder.style.display = 'flex';
                previewImage.src = '';
                previewImage.style.display = 'none';
                base64Output.value = '';
                htmlCodeOutput.value = '';
            });

            // 显示模态框
            function showModal(message) {
                modalMessage.textContent = message;
                modal.style.display = 'block';
            }

            // 关闭模态框
            closeBtn.addEventListener('click', function () {
                modal.style.display = 'none';
            });

            // 点击模态框外部关闭
            window.addEventListener('click', function (event) {
                if (event.target === modal) {
                    modal.style.display = 'none';
                }
            });
        });
    </script>
</body>
</html>